package com.mf.controller;

import com.google.code.kaptcha.Producer;
import com.mf.basic.BasicDuration;
import com.mf.basic.BasicKey;
import com.mf.enums.SendCodeEnum;
import com.mf.exceptions.GraceException;
import com.mf.result.Result;
import com.mf.utils.EnumUtils;
import com.mf.utils.RedisOperator;
import com.mf.utils.VerifyIsErrorUtils;
import com.mf.utils.VerifyUtils;
import com.request.CheckedCodeRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @Author: lmf
 * @Create: 2024/7/19 8:16
 * Description: 验证码
 */
@Slf4j
@RestController
@RequestMapping("/api/notify/v1")
@Tag(name = "验证码")
public class NotifyController {

    @Resource
    private Producer captchaProducer;

    @Resource
    private RedisOperator redisOperator;
    @Operation(summary = "获取图像验证码 地址浏览器可直接访问")
    @GetMapping("getCaptcha")
    public void getCaptcha(@RequestParam("type") String type, HttpServletRequest request,
                           HttpServletResponse response) {
        if(!EnumUtils.existsInEnum(SendCodeEnum.class, type)){
            GraceException.display("验证码类型错误");
        }

        String captchaProducerText = captchaProducer.createText();
        // 存储到redis
        redisOperator.setValueAddTimestamp(BasicKey.getCaptchaKey(request,type), captchaProducerText,
                BasicDuration.CAPTCHA_DURATION);

        BufferedImage image = captchaProducer.createImage(captchaProducerText);
        System.out.println(BasicKey.getCaptchaKey(request,type)+"BasicKey.getCaptchaKey(request,type)");
        try {
            ServletOutputStream outputStream1 = response.getOutputStream();
            ImageIO.write(image, "jpg", outputStream1);
            outputStream1.flush();
            outputStream1.close();
        } catch (IOException e) {
            log.error("获取图像验证码失败", e);
        }
    }

    /**
     * 验证验证码
     */
    @Operation(summary = "RPC-验证验证码是否正确")
    @PostMapping("checkedCode")
    public Boolean checkedCode(@Valid @RequestBody CheckedCodeRequest checkedCodeRequest) {

        String code = checkedCodeRequest.getCaptchaCode();
        String type = checkedCodeRequest.getType();
        String redisKey = redisOperator.get(checkedCodeRequest.getCaptchaKey());

        if(VerifyUtils.isEmpty(redisKey) || !EnumUtils.existsInEnum(SendCodeEnum.class, type)){
            return false;
        }

        String[] split = redisKey.split(redisOperator.SPLIT);
        return split[0].equals(code) ;
    }

    @Operation(summary = "RPC-删除验证码")
    @GetMapping("deleteCode")
    public Result<Boolean> deleteCode(HttpServletRequest request,@RequestParam("type") String type) {
        String captchaKey = BasicKey.getCaptchaKey(request,type);
        redisOperator.delete(captchaKey);
        return Result.success(true);
    }
}
